package com.ideal.manage.esms.controller.project;

import com.ideal.manage.esms.bean.DTO.PageDto;
import com.ideal.manage.esms.bean.DTO.Result;
import com.ideal.manage.esms.bean.project.Message;
import com.ideal.manage.esms.bean.project.ProjectManage;
import com.ideal.manage.esms.bean.project.TemperatureConfig;
import com.ideal.manage.esms.service.project.EquipmentManageService;
import com.ideal.manage.esms.service.project.ProjectManageService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 83730 on 2018/4/17.
 * Remark: 工程配置 -> 对象管理 Controller
 */
@Controller
@RequestMapping("project/objectManage")
public class ObjectManageController {
    private Logger logger = LoggerFactory.getLogger(ObjectManageController.class);

    @Resource
    private ProjectManageService projectManageService;

    @Resource
    private EquipmentManageService equipmentManageService;

    /**
     * 对象管理外面的边框
     */
    @RequestMapping("object_list")
    public void objectList() {
    }

    /**
     * 工程配置 -> 对象管理 -> 项目管理
     */
    @RequestMapping("project_list")
    public void projectList() {
        logger.info("工程配置 -> 对象管理 -> 项目管理");
    }

    /**
     * 工程配置 -> 对象管理 -> 项目管理 -> 数据表
     * @param request
     * @param pageNum
     * @return
     */
    @RequestMapping("project_data")
    @ResponseBody
    public PageDto projectData(int pageNum, HttpServletRequest request) {
        PageDto pageDto = projectManageService.findAll(pageNum, request);
        return pageDto;
    }

    /**
     * 工程配置 -> 对象管理 -> 项目管理 -> 新增/修改
     * @param id
     * @param model
     */
    @RequestMapping("add_project")
    public void addProject(@RequestParam(required = false) String id, Model model) {
        if (StringUtils.isNotBlank(id)) {
            model.addAttribute("id", id);
            ProjectManage projectManage = projectManageService.findById(id);
            model.addAttribute("projectManage", projectManage);
        }
    }

    /**
     * 工程配置 -> 对象管理 -> 项目管理 -> 保存
     * @param id
     * @param name
     * @param ip
     * @param uploadCycle
     * @return
     */
    @RequestMapping("save_project")
    @ResponseBody
    public Result saveProject(@RequestParam(required = false) String id,
                              String name, String ip, Long uploadCycle){
        projectManageService.saveProject(id, name, ip, uploadCycle);
        Result result = new Result();
        result.setType("alert");
        result.setMessage("保存成功");
        return result;
    }

    /**
     * 工程配置 -> 对象管理 -> 项目管理 -> 删除
     * @param ids
     * @return
     */
    @RequestMapping("delete_project")
    @ResponseBody
    public Result deleteProject(String[] ids){
        if (ids.length < 1){
            Result result = new Result();
            result.setType("alert");
            result.setMessage("未能正确获取项目id");
            return result;
        } else {
            projectManageService.deleteProject(ids);
            Result result = new Result();
            result.setType("alert");
            result.setMessage("删除成功");
            return result;
        }
    }

    /**
     * Dai 2018-04-11
     * 工程配置 -> 对象管理 -> 编辑设备树
     * @param model
     */
    @RequestMapping("project_edit")
    public void projectEdit(Model model, String id) {
        model.addAttribute("id", id);       //设备树的id
        List<String> list = equipmentManageService.findIdListByProjectId(id);
        model.addAttribute("list", list);   //变流器和电池包的id
        List ztreeDtos = projectManageService.findZtreeDtoById(id);
        ztreeDtos = equipmentManageService.findAllByProjectId(id, ztreeDtos);
        model.addAttribute("ztreeDtos", ztreeDtos);   //变流器和电池包的id
    }

    /**
     * 工程配置 -> 对象管理 -> 编辑设备树 -> 保存重命名的子节点
     * @param parent_id
     * @param project_id
     * @param name
     * @return
     */
    @RequestMapping("save_equipment_name")
    @ResponseBody
    public List save_equipment_name(String parent_id, String project_id, String name, String type, String id){
        equipmentManageService.saveEquipmentName(parent_id, project_id, name, type, id);
        List ztreeDtos = projectManageService.findZtreeDtoById(project_id);
        ztreeDtos = equipmentManageService.findAllByProjectId(project_id, ztreeDtos);
        return ztreeDtos;
    }
    /**
     * 工程配置 -> 对象管理 -> 编辑设备树 -> 删除
     * @param id
     * @return
     */
    @RequestMapping("delete_equipment")
    public void delete_equipment(String id){
        //TODO
    }

    /**
     * Dai 2018-04-11
     * 工程配置 -> 对象管理 -> 编辑设备树 -> 批量添加变流器和电池包
     */
    @RequestMapping("project_add_list")
    public void project_add_list() {
    }

    @RequestMapping("project_edit_configure")
    public void project_edit_configure(String id){}

    @RequestMapping("project_edit_list")
    public void project_edit_list(String id){}

// ---------------------------------------------------------------------------------------- 项目管理 End

    /**
     * 工程配置 -> 对象管理 -> 设备管理
     */
    @RequestMapping("equipment_list")
    public void equipmentList() {
        logger.info("工程配置 -> 对象管理 -> 设备管理");
    }

    /**
     * 工程配置 -> 对象管理 -> 设备管理 -> 数据表
     * @param pageNum
     * @param request
     * @return
     */
    @RequestMapping("equipment_data")
    @ResponseBody
    public PageDto equipmentData(int pageNum, HttpServletRequest request) {
        PageDto pageDto = equipmentManageService.findAll(pageNum, request);
        return pageDto;
    }

    @RequestMapping("message_list")
    public void messageList(Model model) {
        System.out.println(1111);
    }

    @RequestMapping("message_data")
    @ResponseBody
    public PageDto messageData(int pageNum, HttpServletRequest request) {
        List<Message> messages = new ArrayList<>();
        Message message = new Message();
        message.setName("BatVoltage");
        message.setCanId("1860cf3");
        message.setEndByte("1");
        message.setEndPoint("5");
        message.setLength("8");
        message.setOffset("0");
        message.setPlatformNumber("C001");
        message.setRate("0.1");
        message.setStartByte("0");
        message.setStartPoint("0");
        message.setType("unsigned");
        message.setStatus("Big Mode");
        message.setUnit("V");

        Message message1 = new Message();
        message1.setName("BatVoltage");
        message1.setCanId("1860cf3");
        message1.setEndByte("1");
        message1.setEndPoint("5");
        message1.setLength("8");
        message1.setOffset("0");
        message1.setPlatformNumber("C001");
        message1.setRate("0.1");
        message1.setStartByte("0");
        message1.setStartPoint("0");
        message1.setType("unsigned");
        message1.setStatus("Big Mode");
        message1.setUnit("V");

        messages.add(message);
        messages.add(message1);

        PageDto pageDto = new PageDto();
        pageDto.setRows(messages);
        pageDto.setTotal(2L);
        return pageDto;
    }

    @RequestMapping("message_add")
    public void messageAdd(Model model) {
        System.out.println(1111);
    }

    @RequestMapping("message_edit")
    public void messageEdit(Model model) {
        System.out.println(1111);
    }

    @RequestMapping("temperatureConfig_list")
    public void temperatureConfigList(Model model) {
    }

    @RequestMapping("config_edit")
    public void configEdit(Model model) {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

        Map<String, Object> m = new HashMap<String, Object>();
        m.put("name", "1#电池单体");
        list.add(m);

        m = new HashMap<String, Object>();
        m.put("name", "2#电池单体");
        list.add(m);

        m = new HashMap<String, Object>();
        m.put("name", "3#电池单体");
        list.add(m);

        m = new HashMap<String, Object>();
        m.put("name", "4#电池单体");
        list.add(m);

        m = new HashMap<String, Object>();
        m.put("name", "5#电池单体");
        list.add(m);
        model.addAttribute("list", list);

        List<Map<String, Object>> list1 = new ArrayList<Map<String, Object>>();

        Map<String, Object> m1 = new HashMap<String, Object>();
        m1.put("name", "1#温度");
        list1.add(m1);

        m1 = new HashMap<String, Object>();
        m1.put("name", "2#温度");
        list1.add(m1);

        m1 = new HashMap<String, Object>();
        m1.put("name", "3#温度");
        list1.add(m1);
        model.addAttribute("list1", list1);
        System.out.println(1111);
    }

    @RequestMapping("config_data")
    @ResponseBody
    public PageDto configData(int pageNum, HttpServletRequest request) {
        List<TemperatureConfig> configs = new ArrayList<>();

        TemperatureConfig config = new TemperatureConfig();
        config.setProject("大拇指广场");
        config.setBatteryPack("1#电池包");
        config.setDetail("未配置");
        config.setName("1#电池模组");

        TemperatureConfig config1 = new TemperatureConfig();
        config1.setProject("国金大厦");
        config1.setBatteryPack("2#电池包");
        config1.setDetail("");
        config1.setName("2#电池模组");

        configs.add(config);
        configs.add(config1);
        PageDto pageDto = new PageDto();
        pageDto.setRows(configs);
        pageDto.setTotal(2L);
        return pageDto;
    }

    @RequestMapping("config_monomer_data")
    @ResponseBody
    public PageDto configMonData(int pageNum, HttpServletRequest request) {
        List<TemperatureConfig> configs = new ArrayList<>();

        TemperatureConfig config = new TemperatureConfig();
        config.setProject("大拇指广场");
        config.setBatteryPack("1#电池包");
        config.setDetail("未配置");
        config.setName("1#电池模组");

        TemperatureConfig config1 = new TemperatureConfig();
        config1.setProject("国金大厦");
        config1.setBatteryPack("2#电池包");
        config1.setDetail("");
        config1.setName("2#电池模组");

        configs.add(config);
        configs.add(config1);
        PageDto pageDto = new PageDto();
        pageDto.setRows(configs);
        pageDto.setTotal(2L);
        return pageDto;
    }

    @RequestMapping("config_temperature_data")
    @ResponseBody
    public PageDto configTemData(int pageNum, HttpServletRequest request) {
        List<TemperatureConfig> configs = new ArrayList<>();

        TemperatureConfig config = new TemperatureConfig();
        config.setProject("大拇指广场");
        config.setBatteryPack("1#电池包");
        config.setDetail("未配置");
        config.setName("1#电池模组");

        TemperatureConfig config1 = new TemperatureConfig();
        config1.setProject("国金大厦");
        config1.setBatteryPack("2#电池包");
        config1.setDetail("");
        config1.setName("2#电池模组");

        configs.add(config);
        configs.add(config1);
        PageDto pageDto = new PageDto();
        pageDto.setRows(configs);
        pageDto.setTotal(2L);
        return pageDto;
    }


}
